package com.sinothk.fc.attend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.attend.entity.AttendConfigAndTodayDataBean;
import com.sinothk.fc.attend.entity.AttendRecordEntity;
import com.sinothk.fc.attend.entity.AttendRecordReq;
import com.sinothk.fc.attend.entity.StatisticsAttendForEmployeeBean;
import com.sinothk.fc.attend.service.IAttendRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 考勤记录表
 * @Author: Liangyt
 * @Date: 2022-09-18
 * @Version: V1.0
 */
@Api(tags = "考勤记录表")
@RestController
@RequestMapping("/fc/attendRecordCtrl")
@Slf4j
public class AttendRecordController extends JeecgController<AttendRecordEntity, IAttendRecordService> {
    @Autowired
    private IAttendRecordService attendRecordService;

    /**
     * 分页列表查询
     *
     * @param attendRecord
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "考勤记录表-分页列表查询")
    @ApiOperation(value = "考勤记录表-分页列表查询", notes = "考勤记录表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AttendRecordEntity attendRecord,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AttendRecordEntity> queryWrapper = QueryGenerator.initQueryWrapper(attendRecord, req.getParameterMap());
        Page<AttendRecordEntity> page = new Page<AttendRecordEntity>(pageNo, pageSize);
        IPage<AttendRecordEntity> pageList = attendRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加或编辑
     *
     * @param attendRecordReq
     * @return
     */
    @AutoLog(value = "考勤记录表-添加或编辑")
    @ApiOperation(value = "考勤记录表-添加或编辑", notes = "考勤记录表-添加或编辑")
    @PostMapping(value = "/addOrEdit")
    public Result<?> addOrEdit(@RequestBody AttendRecordReq attendRecordReq) throws Exception {
        attendRecordService.addOrEdit(attendRecordReq);
        return Result.OK("操作成功");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "考勤记录表-通过id查询")
    @ApiOperation(value = "考勤记录表-通过id查询", notes = "考勤记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AttendRecordEntity attendRecord = attendRecordService.getById(id);
        if (attendRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(attendRecord);
    }

    /**
     * 查询考勤配置和今天的考勤数据
     *
     * @return
     */
    @AutoLog(value = "考勤记录表-通过id查询")
    @ApiOperation(value = "考勤记录表-通过id查询", notes = "考勤记录表-通过id查询")
    @GetMapping(value = "/getAttendConfigAndTodayData")
    public Result<?> getAttendConfigAndTodayData() throws Exception {
        AttendConfigAndTodayDataBean attendConfigAndTodayDataBean = attendRecordService.getAttendConfigAndTodayData();
        return Result.OK(attendConfigAndTodayDataBean);
    }


    /**
     * 导出excel
     *
     * @param request
     * @param attendRecord
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AttendRecordEntity attendRecord) {
        return super.exportXls(request, attendRecord, AttendRecordEntity.class, "考勤记录表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AttendRecordEntity.class);
    }

    /**
     * 考勤统计-本人今日考勤
     *
     * @return
     */
    @AutoLog(value = "考勤统计-本人今日考勤")
    @ApiOperation(value = "考勤统计-本人今日考勤", notes = "考勤统计-本人今日考勤")
    @GetMapping(value = "/statisticsForCurrUser")
    public Result<?> statisticsForCurrUser() throws Exception {
        StatisticsAttendForEmployeeBean statisticsAttend = attendRecordService.statisticsForCurrUser();
        return Result.OK(statisticsAttend);
    }

    /**
     * 考勤统计-员工今日考勤
     *
     * @return
     */
    @AutoLog(value = "考勤统计-员工今日考勤")
    @ApiOperation(value = "考勤统计-员工今日考勤", notes = "考勤统计-员工今日考勤")
    @GetMapping(value = "/statisticsForEmployee")
    public Result<?> statisticsForEmployee() throws Exception {
        StatisticsAttendForEmployeeBean statisticsAttend = attendRecordService.statisticsForEmployee();
        return Result.OK(statisticsAttend);
    }
}
